﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMS.DAL;
using SMS.Business.Items;
using SMS.DAL.Providers;
using SMS.Business;

namespace SMS.Items
{
    public partial class ItemSearch : System.Web.UI.Page
    {
        ItemBusiness ItemBus;
        List<Item> ItemsList
        {
            get
            {
                if ((List<Item>)ViewState["ItemsList"] != null)
                    return (List<Item>)ViewState["ItemsList"];
                else
                    return new List<Item>();
            }
            set
            {
                if ((List<Item>)ViewState["ItemsList"] != null)
                    ViewState["ItemsList"] = value;
                else
                    ViewState["ItemsList"] = new List<Item>();
            }
        }
        User CurrentUser
        {
            get
            {
                return (User)Session["CurrentUser"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ItemBus = new ItemBusiness();
            ItemsList = new List<Item>();
            ItemSearchCriteria ItemSearch = new ItemSearchCriteria()
            {
                Barcode=txtBarCode.Text,
                Mark = txtMark.Text,
                Name = txtName.Text,
                PartNumber = txtPartNumber.Text
            };
            ItemsList = ItemBus.UserSearch(ItemSearch);
            BindgvItems();
        }

        protected void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvItems.PageIndex = e.NewPageIndex;
            BindgvItems();
        }

        private void BindgvItems()
        {
            gvItems.DataSource = ItemsList;
            gvItems.DataBind();
        }

        protected void gvItems_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int ItemID = (int)gvItems.DataKeys[e.NewEditIndex].Value;
            Response.Redirect("~/Items/UpdateItem.aspx?ItemID=" + ItemID);
        }
    }
}
